const axios = require('axios').default
const http = axios.create({
  // baseURL: 'https://httpbin.org/',
  proxy: false,
})

// const httpsAgent = new SocksProxyAgent.SocksProxyAgent('')
// 请求拦截器
http.interceptors.request.use(async (config) => {
  if(Boolean(config.noProxy) === false) {
    // [Request to HTTPS with HTTP proxy fails](https://github.com/axios/axios/issues/925#issuecomment-359982190)
    // config.httpsAgent = await new require('https-proxy-agent')(`http://${await getProxyKuaiIp()}`)
    // config.httpsAgent = await new require('https-proxy-agent')(`http://111.178.2.1:8088/`)
    // config.httpAgent = httpsAgent
    config.headers[`User-Agent`] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
    // config.headers[`Accept-Encoding`] = "gzip"
    config.headers[`Referer`] = "http://www.innojoy.com/searchresult/default.html"
    config.headers[`Content-Type`] = "application/json"
    config.headers[`Origin`] = "http://www.innojoy.com"
    config.headers[`Cookie`] = "__yjs_duid=1_b40e1140c931fba7e9e6c6705111ea9b1678349725949; Hm_lvt_426d1726327b6b6225922c5623453d3d=1678349732; DWJSID=node0lgrzplgct7k41p3ai8uzlo19w25817.node0; Hm_lpvt_426d1726327b6b6225922c5623453d3d=1678415233"
    // config.headers[`Host`] = "http://www.innojoy.com"
  }
  
  return config
}, (err) => Promise.reject(err))


// 响应拦截器
http.interceptors.response.use((res) => res.data, (err) => Promise.reject(err))



// 获取代理服务器
async function getProxyIp() {
  const data = await http.get(`http://demo.spiderpy.cn/get/?type=https`, {noProxy: true})
  return data.proxy
}


module.exports = http